﻿
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Linq
Imports TNCBAL
Imports TNCDAL
Imports System.Text.RegularExpressions
Imports System.Text
Imports System.Globalization

Public Class Utilities
    Private db As New TanNamChinhDataContext

    Public Function GetFileContents(ByVal FullPath As String, _
           Optional ByRef ErrInfo As String = "") As String

        Dim strContents As String
        Dim objReader As StreamReader
        Try

            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            objReader.Close()
            Return strContents
        Catch Ex As Exception
            ErrInfo = Ex.Message
            Return ""
        End Try
    End Function
    Public Function GetKursDollar() As Double
        Try
            Dim query = "" ' db.fn_get_currencies("USD")

            Return query
        Catch ex As Exception
            Return 0
        End Try
    End Function
    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean

        ' Dim Contents As String
        Dim bAns As Boolean = False
        Dim objReader As StreamWriter
        Try


            objReader = New StreamWriter(FullPath)
            objReader.Write(strData)
            objReader.Close()
            bAns = True
        Catch Ex As Exception
            ErrInfo = Ex.Message

        End Try
        Return bAns
    End Function
    Public Function LinqToTable(ByVal ctx As Linq.DataContext, ByVal query As Object) As DataTable
        Dim cmd As IDbCommand = ctx.GetCommand(query)
        Dim adapter As SqlDataAdapter = New SqlDataAdapter()
        adapter.SelectCommand = CType(cmd, SqlCommand)

        Dim dt As DataTable = New DataTable("sd")

        Try

            cmd.Connection.Open()
            adapter.FillSchema(dt, SchemaType.Source)
            adapter.Fill(dt)

        Finally
            cmd.Connection.Close()
        End Try
        Return dt
    End Function

    'Function searchCustomer(ByVal name As String) As Integer
    '    Try
    '        Dim cust As customer
    '        cust = db.customers.Single(Function(item) item.name.Contains(name))
    '        If IsNothing(cust) Then
    '            Return 0
    '        Else
    '            Return cust.id
    '        End If
    '    Catch ex As Exception
    '        Return 0
    '    End Try

    'End Function
    Function searchDriver(ByVal name As String) As Integer
        Try
            Dim driv As tblDriver

            driv = db.tblDrivers.Single(Function(item) item.DriverName.Contains(name))
            If IsNothing(driv) Then
                Return 0
            Else
                Return driv.ID_Driver

            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function
    'Function getOfferNo(ByVal offerid As Integer) As String
    '    Try
    '        Dim offers As transact_offer
    '        offers = db.transact_offers.Single(Function(item) item.id = offerid)
    '        If IsNothing(offers) Then
    '            Return ""
    '        Else
    '            Return offers.offer_no

    '        End If
    '    Catch ex As Exception
    '        Return ""
    '    End Try
    'End Function
    'Function getOfferID(ByVal offerno As String) As Integer
    '    Try
    '        Dim offers As transact_offer
    '        offers = db.transact_offers.Single(Function(item) item.offer_no.Contains(offerno))
    '        If IsNothing(offers) Then
    '            Return 0
    '        Else
    '            Return offers.id

    '        End If
    '    Catch ex As Exception
    '        Return 0
    '    End Try


    'End Function
    'Function isOfferNoExist(ByVal offerno As String) As Boolean
    '    If getOfferID(offerno) > 0 Then
    '        Return True
    '    Else
    '        Return False
    '    End If
    'End Function
    'Function getNomorOrder(ByVal orderid As Integer) As String
    '    Try
    '        Dim orders As transact_order
    '        orders = db.transact_orders.Single(Function(item) item.id = orderid)
    '        If IsNothing(orders) Then
    '            Return ""
    '        Else
    '            Return orders.order_no

    '        End If
    '    Catch ex As Exception
    '        Return ""
    '    End Try
    'End Function
    'Function getOrderID(ByVal orderno As String) As Integer
    '    Try
    '        Dim orders As transact_order
    '        orders = db.transact_orders.Single(Function(item) item.order_no.Contains(orderno))
    '        If IsNothing(orders) Then
    '            Return 0
    '        Else
    '            Return orders.id

    '        End If
    '    Catch ex As Exception
    '        Return 0
    '    End Try


    'End Function
    Sub ShowInfo(ByVal Title As String, ByVal Msg As String)
        MessageBox.Show(Msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub
    Sub ShowError(ByVal Title As String, ByVal Msg As String)
        MessageBox.Show(Msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Error)

    End Sub
    Function ShowConfirm(ByVal Title As String, ByVal msg As String) As Integer
        Return MessageBox.Show(msg, Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    End Function
    Sub FillContTypeCombo(ByVal combo As DataGridViewComboBoxColumn)
        Dim query As Object


        query = From a In db.tblContTypes
                        Select a.ID_ContType, a.ContTypeName
                        Order By ContTypeName Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "ContTypeName"
                .ValueMember = "ID_ContType"
            End With
        End If

    End Sub
    Sub FillContTypeCombo(ByVal combo As ComboBox)
        Dim query As Object


        query = From a In db.tblContTypes
                        Select a.ID_ContType, a.ContTypeName
                        Order By ContTypeName Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "ContTypeName"
                .ValueMember = "ID_ContType"
            End With
        End If

    End Sub
    Sub FillFileCombo(ByVal combo As ComboBox)
        Dim query As Object


        query = From a In db.tblFiles
                        Select a.ID_File, a.FileNo
                        Order By FileNo Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "FileNo"
                .ValueMember = "ID_File"
            End With
        End If

    End Sub
    Sub FillAllFileCombo(ByVal combo As ComboBox)
        Dim query As Object
        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing
                Select a


        Dim emptyTable As tblFile() = {New tblFile With {.ID_File = 0, .FileNo = 0}}
        Dim files = (From e In emptyTable Select e).Union(query).ToList()

        If Not IsNothing(files) Then
            With combo
                .DataSource = files
                .DisplayMember = "FileNo"
                .ValueMember = "ID_File"
            End With
        End If

    End Sub
    Sub FillAllFileCombo(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree)
        Dim query As Object
        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing
                Select a



        Dim emptyTable As tblFile() = {New tblFile With {.ID_File = 0, .FileNo = 0}}
        Dim files = (From e In emptyTable Select e).Union(query).ToList()


        With combo
            .DataSource = query
            .DisplayMembers = "FileNo,CreatedDate"
            .SelectedDisplayMemeber = "FileNo"
            .ValueMember = "ID_File"
        End With

    End Sub
    Sub FillAllFileComboTree(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree)
        Dim query As Object

        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing
                Order By a.FileNo Descending
                Select a.ID_File, a.FileNo, a.CreatedDate, a.IsExport, a.tblCustomer.CustomerName


        With combo
            .DataSource = query
            .DisplayMembers = "FileNo,CreatedDate"
            .SelectedDisplayMemeber = "FileNo"
            .ValueMember = "ID_File"
            .GroupingMembers = "IsExport,CustomerName"
        End With

    End Sub
    ''' <summary>
    ''' Lấy danh sách tất cả file import or export
    ''' </summary>
    ''' <param name="combo"></param>
    ''' <param name="isEx"></param>
    ''' <remarks></remarks>
    Sub FillAllFileComboTree(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree, ByVal isEx As Boolean)
        Dim query As Object

        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing And a.IsExport = isEx
                Order By a.ID_File Descending
                Select a.ID_File, a.FileNo, a.CreatedDate, a.IsExport, a.tblCustomer.CustomerName


        With combo
            .DataSource = query
            .DisplayMembers = "FileNo,CreatedDate"
            '.SelectedDisplayMemeber = "FileNo"
            .ValueMember = "ID_File"
            '.GroupingMembers = "CustomerName"
        End With

    End Sub
    ''' <summary>
    ''' Danh sách các file đã được chi rồi
    ''' </summary>
    ''' <param name="combo"></param>
    ''' <param name="isEx"></param>
    ''' <remarks></remarks>
    Sub FillAllFileFinishPaymentComboTree(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree, ByVal isEx As Boolean)
        Dim query As Object
        'Lấy danh sách Phí có thể chi
        Dim listFee = From a In db.tblFees
                  Where a.ID_FeeType = 1
                  Select a.ID_Fee

        'Lấy danh sách File đã có chi và số phí chi bằng số lượng đã có trên
        Dim listFileFinish = (From a In db.tblFileFees
                             Where Not (a.ID_AdvanceVoucher.HasValue) And a.ID_PaymentVoucher.HasValue
                             Group By a.ID_File Into myGroup = Count(a.ID_Fee)
                                Where myGroup <= listFee.Count
                                Select ID_File).ToList


        'Lấy danh sách File
        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing And a.IsExport = isEx And listFileFinish.Contains(a.ID_File)
                Order By a.ID_File Descending
                Select a.ID_File, a.FileNo

        With combo
            .DataSource = query
            .DisplayMembers = "FileNo"
            '.SelectedDisplayMemeber = "FileNo"
            .ValueMember = "ID_File"
            '.GroupingMembers = "CustomerName"
        End With

    End Sub
    ''' <summary>
    ''' Danh sách các file chưa chi
    ''' </summary>
    ''' <param name="combo"></param>
    ''' <param name="isEx"></param>
    ''' <remarks></remarks>
    Sub FillAllFileForPaymentComboTree(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree, ByVal isEx As Boolean)
        Dim query As Object
        'Lấy danh sách Phí có thể chi
        Dim listFee = From a In db.tblFees
                  Where a.ID_FeeType = 1
                  Select a.ID_Fee

        ''Lấy danh sách File đã có chi và số phí chi bằng số lượng đã có trên
        'Dim listFileFinish = (From a In db.tblFileFees
        '                     Where Not (a.ID_AdvanceVoucher.HasValue) And a.ID_PaymentVoucher.HasValue
        '                     Group By a.ID_File Into myGroup = Count(a.ID_Fee)
        '                    Where (myGroup = listFee.Count)
        '                        Select ID_File).ToList

        'Lấy danh sách File đã có chi và số phí chi bằng số lượng đã có trên
        Dim listFileFinish = (From a In db.tblFileFees
                             Where Not (a.ID_AdvanceVoucher.HasValue) And a.ID_PaymentVoucher.HasValue
                           Where listFee.Contains(a.ID_Fee)
                            Select a.ID_File).ToList

        'Lấy danh sách File chưa chi
        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing And a.IsExport = isEx And Not listFileFinish.Contains(a.ID_File)
                Order By a.ID_File Descending
                Select a.ID_File, a.FileNo


        With combo
            .DataSource = query
            .DisplayMembers = "FileNo"
            '.SelectedDisplayMemeber = "FileNo"
            .ValueMember = "ID_File"
            '.GroupingMembers = "CustomerName"
        End With

    End Sub
    ''' <summary>
    ''' Danh sách các file chưa chi
    ''' </summary>
    ''' <param name="combo"></param>
    ''' <param name="isEx"></param>
    ''' <remarks></remarks>
    Sub FillAllFileForPaymentTextbox(ByVal textbox As TextBox, ByVal isEx As Boolean)
        Dim query As Object
        'Lấy danh sách Phí có thể chi
        Dim listFee = From a In db.tblFees
                  Where a.ID_FeeType = 1
                  Select a.ID_Fee

        ''Lấy danh sách File đã có chi và số phí chi bằng số lượng đã có trên
        'Dim listFileFinish = (From a In db.tblFileFees
        '                     Where Not (a.ID_AdvanceVoucher.HasValue) And a.ID_PaymentVoucher.HasValue
        '                     Group By a.ID_File Into myGroup = Count(a.ID_Fee)
        '                    Where (myGroup = listFee.Count)
        '                        Select ID_File).ToList

        'Lấy danh sách File đã có chi và số phí chi bằng số lượng đã có trên
        Dim listFileFinish = (From a In db.tblFileFees
                             Where Not (a.ID_AdvanceVoucher.HasValue) And a.ID_PaymentVoucher.HasValue
                           Where listFee.Contains(a.ID_Fee)
                            Select a.ID_File).ToList

        'Lấy danh sách File chưa chi
        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing And a.IsExport = isEx And Not listFileFinish.Contains(a.ID_File)
                Order By a.ID_File Descending
                Select a.FileNo
        Dim source = New AutoCompleteStringCollection()

        For Each obj As String In query
            source.Add(obj)
        Next

        Try
            textbox.AutoCompleteCustomSource = source
            textbox.AutoCompleteMode = AutoCompleteMode.Suggest
            textbox.AutoCompleteSource = AutoCompleteSource.CustomSource
        Catch ex As Exception
            Throw
        End Try


    End Sub
    ''' <summary>
    ''' Danh sách các file chưa chi
    ''' </summary>
    ''' <param name="combo"></param>
    ''' <param name="isEx"></param>
    ''' <remarks></remarks>
    Sub FillAllFileForPaymentComboBox(ByVal combo As ComboBox, ByVal isEx As Boolean)
        Dim query As Object
        'Lấy danh sách Phí có thể chi
        Dim listFee = From a In db.tblFees
                  Where a.ID_FeeType = 1
                  Select a.ID_Fee

        ''Lấy danh sách File đã có chi và số phí chi bằng số lượng đã có trên
        'Dim listFileFinish = (From a In db.tblFileFees
        '                     Where Not (a.ID_AdvanceVoucher.HasValue) And a.ID_PaymentVoucher.HasValue
        '                     Group By a.ID_File Into myGroup = Count(a.ID_Fee)
        '                    Where (myGroup = listFee.Count)
        '                        Select ID_File).ToList

        'Lấy danh sách File đã có chi và số phí chi bằng số lượng đã có trên
        Dim listFileFinish = (From a In db.tblFileFees
                             Where Not (a.ID_AdvanceVoucher.HasValue) And a.ID_PaymentVoucher.HasValue
                           Where listFee.Contains(a.ID_Fee)
                            Select a.ID_File).ToList

        'Lấy danh sách File chưa chi
        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing And a.IsExport = isEx And Not listFileFinish.Contains(a.ID_File)
                Order By a.ID_File Descending
                Select a.ID_File, a.FileNo


        With combo
            .DataSource = query
            .DisplayMember = "FileNo"
            '.SelectedDisplayMemeber = "FileNo"
            .ValueMember = "ID_File"
            '.GroupingMembers = "CustomerName"
        End With

    End Sub

    ''' <summary>
    ''' Danh sách các file chưa được tạm ứng hoặc mới tạm ứng một phần
    ''' </summary>
    ''' <param name="combo"></param>
    ''' <param name="isEx"></param>
    ''' <remarks></remarks>
    Sub FillAllFileForAdvancedComboTree(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree, ByVal isEx As Boolean)
        Dim query As Object
        'Lấy danh sách Phí có thể tạm ứng
        Dim listFee = From a In db.tblFees
                  Where a.ID_FeeType = 1
                  Select a.ID_Fee

        'Lấy danh sách File đã có tạm ứng và số phí tạm ứng bằng số lượng đã có trên
        Dim listFileFinish = (From a In db.tblFileFees
                             Where a.ID_AdvanceVoucher.HasValue
                             Group By a.ID_File Into myGroup = Count(a.ID_AdvanceVoucher)
                                Where myGroup = listFee.Count
                                Select ID_File).ToList

        Dim testlist = From a In db.tblFileFees
                       Where a.ID_AdvanceVoucher.HasValue
                       Select a



        'Lấy danh sách File
        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing And a.IsExport = isEx And Not listFileFinish.Contains(a.ID_File)
                Order By a.tblCustomer.CustomerName, a.FileNo Descending
                Select a.ID_File, a.FileNo, a.CreatedDate, a.IsExport, a.tblCustomer.CustomerName
        'query = From a In db.tblFiles
        '        Where a.IsExport = isEx
        '        Order By a.ID_File Descending
        '        Select a.ID_File, a.FileNo, a.CreatedDate



        With combo
            .DataSource = query
            .DisplayMembers = "FileNo"
            .ValueMember = "ID_File"
        End With

    End Sub

   
    Sub FillAllFileForAdvancedTextbox(ByVal tbox As TextBox, ByVal isEx As Boolean)
        Dim query As Object
        'Lấy danh sách Phí có thể tạm ứng
        Dim listFee = From a In db.tblFees
                  Where a.ID_FeeType = 1
                  Select a.ID_Fee

        'Lấy danh sách File đã có tạm ứng và số phí tạm ứng bằng số lượng đã có trên
        Dim listFileFinish = (From a In db.tblFileFees
                             Where a.ID_AdvanceVoucher.HasValue
                             Group By a.ID_File Into myGroup = Count(a.ID_AdvanceVoucher)
                                Where myGroup = listFee.Count
                                Select ID_File).ToList

        Dim testlist = From a In db.tblFileFees
                       Where a.ID_AdvanceVoucher.HasValue
                       Select a

        'Lấy danh sách File
        query = (From a In db.tblFiles
                Where a.FileNo IsNot Nothing And a.IsExport = isEx And Not listFileFinish.Contains(a.ID_File)
                Order By a.tblCustomer.CustomerName, a.FileNo Descending
                Select a.FileNo).ToList
        'query = From a In db.tblFiles
        '        Where a.IsExport = isEx
        '        Order By a.ID_File Descending
        '        Select a.ID_File, a.FileNo, a.CreatedDate
        Dim source = New AutoCompleteStringCollection()

        For Each obj As String In query
            source.Add(obj)
        Next
        With tbox
            .AutoCompleteCustomSource = source
            .AutoCompleteMode = AutoCompleteMode.Suggest
            .AutoCompleteSource = AutoCompleteSource.CustomSource
        End With

    End Sub
    ''' <summary>
    ''' Danh sách các file chưa được tạm ứng hoặc mới tạm ứng một phần
    ''' </summary>
    ''' <param name="combo"></param>
    ''' <param name="isEx"></param>
    ''' <remarks></remarks>
    Sub FillAllFileForAdvancedComboBox(ByVal combo As DevComponents.DotNetBar.Controls.ComboBoxEx, ByVal isEx As Boolean)
        Dim query As Object
        'Lấy danh sách Phí có thể tạm ứng
        Dim listFee = From a In db.tblFees
                  Where a.ID_FeeType = 1
                  Select a.ID_Fee

        'Lấy danh sách File đã có tạm ứng và số phí tạm ứng bằng số lượng đã có trên
        Dim listFileFinish = (From a In db.tblFileFees
                             Where a.ID_AdvanceVoucher.HasValue
                             Group By a.ID_File Into myGroup = Count(a.ID_AdvanceVoucher)
                                Where myGroup = listFee.Count
                                Select ID_File).ToList

        Dim testlist = From a In db.tblFileFees
                       Where a.ID_AdvanceVoucher.HasValue
                       Select a



        'Lấy danh sách File
        'query = From a In db.tblFiles
        '        Where a.FileNo IsNot Nothing And a.IsExport = isEx And Not listFileFinish.Contains(a.ID_File)
        '        Order By a.tblCustomer.CustomerName, a.FileNo Descending
        '        Select a.ID_File, a.FileNo, a.CreatedDate, a.IsExport, a.tblCustomer.CustomerName
        query = From a In db.tblFiles
                Where a.IsExport = isEx
                Select a.ID_File, a.FileNo, a.IsExport

        With combo
            .DataSource = query
            .DisplayMember = "FileNo"
            .ValueMember = "ID_File"
        End With

    End Sub
    ''' <summary>
    ''' Danh sách các file đã được tạm ứng rồi
    ''' </summary>
    ''' <param name="combo"></param>
    ''' <param name="isEx"></param>
    ''' <remarks></remarks>
    Sub FillAllFileFinishAdvanceComboTree(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree, ByVal isEx As Boolean)
        Dim query As Object
        'Lấy danh sách Phí có thể tạm ứng
        Dim listFee = From a In db.tblFees
                  Where a.ID_FeeType = 1
                  Select a.ID_Fee

        'Lấy danh sách File đã có tạm ứng và số phí tạm ứng bằng số lượng đã có trên
        Dim listFileFinish = (From a In db.tblFileFees
                             Where a.ID_AdvanceVoucher.HasValue
                             Group By a.ID_File Into myGroup = Count(a.ID_Fee)
                                Where myGroup <= listFee.Count
                                Select ID_File).ToList


        'Lấy danh sách File
        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing And a.IsExport = isEx And listFileFinish.Contains(a.ID_File)
                Order By a.tblCustomer.CustomerName, a.FileNo Descending
                Select a.ID_File, a.FileNo, a.CreatedDate, a.IsExport, a.tblCustomer.CustomerName


        With combo
            .DataSource = query
            .DisplayMembers = "FileNo,CreatedDate"
            .SelectedDisplayMemeber = "FileNo"
            .ValueMember = "ID_File"
            .GroupingMembers = "CustomerName"
        End With

    End Sub
    ''' <summary>
    ''' Danh sách các file đã được tạm ứng rồi
    ''' </summary>
    ''' <param name="combo"></param>
    ''' <param name="isEx"></param>
    ''' <remarks></remarks>
    Sub FillAllFileFinishAdvanceComboBox(ByVal combo As DevComponents.DotNetBar.Controls.ComboBoxEx, ByVal isEx As Boolean)
        Dim query As Object
        'Lấy danh sách Phí có thể tạm ứng
        Dim listFee = From a In db.tblFees
                  Where a.ID_FeeType = 1
                  Select a.ID_Fee

        'Lấy danh sách File đã có tạm ứng và số phí tạm ứng bằng số lượng đã có trên
        Dim listFileFinish = (From a In db.tblFileFees
                             Where a.ID_AdvanceVoucher.HasValue
                             Group By a.ID_File Into myGroup = Count(a.ID_Fee)
                                Where myGroup <= listFee.Count
                                Select ID_File).ToList


        'Lấy danh sách File
        query = From a In db.tblFiles
                Where a.FileNo IsNot Nothing And a.IsExport = isEx And listFileFinish.Contains(a.ID_File)
                Order By a.tblCustomer.CustomerName, a.FileNo Descending
                Select a.ID_File, a.FileNo


        With combo
            .DataSource = query
            .DisplayMember = "FileNo"
            .ValueMember = "ID_File"

        End With

    End Sub
    Sub FillAllCustomerCombo(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree, ByVal isExport As Boolean)
        '========================
        'HT Code: xử lý lại isExport
        '========================
        Dim query As Object
        'query = From a In db.tblCustomers
        '        Where a.IsActive = True And (a.tblCompany.IsExport = value Or a.tblCompany.isExport = 2)
        '        Order By a.CustomerName
        '        Select a.ID_Customer, a.CustomerName, a.Tel
        Dim objCustomerController As New CustomerController
        query = objCustomerController.SelectCustomAllCustomers()

        With combo
            .DataSource = query
            .DisplayMembers = "CustomerName,Tel"
            .SelectedDisplayMemeber = "CustomerName"
            .ValueMember = "ID_Customer"
            ' .GroupingMembers = "CompanyName"
        End With

    End Sub
    Sub FillCustomerCombo(ByVal combo As ComboBox)
        Dim query As Object


        query = From a In db.tblCustomers
                        Select a.ID_Customer, a.CustomerName
                        Order By CustomerName Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "CustomerName"
                .ValueMember = "ID_Customer"
            End With
        End If

    End Sub
    Sub FillCustomerAllCombo(ByVal combo As ComboBox)
        Dim customer1 = From a In db.tblCustomers
                       Select a
        Dim emptyTable As tblCustomer() = {New tblCustomer With {.ID_Customer = 0, .CustomerName = "------"}}
        Dim customer = (From e In emptyTable Select e).Union(customer1).ToList
        With combo
            .DataSource = customer
            .DisplayMember = "CustomerName"
            .ValueMember = "ID_Customer"
        End With
    End Sub


    Sub FillCompanyCombo(ByVal combo As ComboBox, ByVal isexport As Boolean)
        Dim query As Object

        '========================
        'HT Code: xử lý lại isExport và kiểm tra isActive = true
        '========================
        'If isexport = True Then
        'nêu xuất khẩu thì lấy công ty xuất khẩu
        Dim objCompanyController As CompanyController
        query = objCompanyController.SelectAllRecord(isexport)
        'Else
        'query = From a In db.tblCompanies
        '        Where a.IsExport = value Or a.isExport = 2
        '           Select a
        'End If


        'Dim emptyTable As tblCompany() = {New tblCompany With {.ID_Company = 0, .CompanyName = "-------"}}
        'Dim company = (From e In emptyTable Select e).Union(query).ToList

        'Order By CompanyName Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "CompanyName"
                .ValueMember = "ID_Company"
            End With
        End If

    End Sub

    Sub FillPortCombo(ByVal combo As ComboBox, ByVal isForeign As Boolean)
        Dim query As Object

        If isForeign Then
            'Nếu là nước ngoài thì hiển thị cảng nước ngoài
            query = From a In db.tblPorts
                    Where a.tblCountry.ID_Country <> 1
                      Select a
        Else
            query = From a In db.tblPorts
                    Where a.tblCountry.ID_Country = 1
                      Select a
        End If

        Dim emptyTable As tblPort() = {New tblPort With {.ID_Port = 0, .Port = "-------"}}
        Dim ports = (From e In emptyTable Select e).Union(query).ToList

        'Order By CompanyName Ascending

        If Not IsNothing(ports) Then
            With combo
                .DataSource = ports
                .DisplayMember = "Port"
                .ValueMember = "ID_Port"
            End With
        End If

    End Sub
    Sub FillForeignCompanyCombo(ByVal combo As ComboBox, ByVal isexport As Boolean)
        Dim query As Object


        query = From a In db.tblForeignCompanies
                      Select a



        ' Order By Name Ascending
        Dim emptyTable As tblForeignCompany() = {New tblForeignCompany With {.ID_ForeignCompany = 0, .Name = "-------"}}
        Dim fcompany = (From e In emptyTable Select e).Union(query).ToList
        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "Name"
                .ValueMember = "ID_ForeignCompany"
            End With
        End If

    End Sub

    Sub FillVesselCombo(ByVal combo As ComboBox, ByVal line As Integer)
        'Dim query As Object

        'If line = 0 Then
        '    'Chọn vessel trước
        '    query = From a In db.tblVessels
        '                Select a.ID_Vessel, a.VesselName
        'Else
        '    'Chọn hãng tàu rồi mới chọn tàu
        '    query = From a In db.tblVessels
        '        Where a.ID_Line = line
        '                Select a.ID_Vessel, a.VesselName
        'End If


        '' Order By Name Ascending

        'If Not IsNothing(query) Then
        '    With combo
        '        .DataSource = query
        '        .DisplayMember = "VesselName"
        '        .ValueMember = "ID_Vessel"
        '    End With
        'End If

    End Sub
    Sub FillLineCombo(ByVal combo As ComboBox)
        Dim query As Object


        query = From a In db.tblLines
                    Select a.ID_Line, a.LineName


        ' Order By Name Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "LineName"
                .ValueMember = "ID_Line"
            End With
        End If

    End Sub
    Sub FillDriverCombo(ByVal combo As ComboBox)
        Dim query As Object

        query = From a In db.tblDrivers
                Where a.IsActive = True
                    Select a.ID_Driver, a.DriverName
                    Order By DriverName Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "DriverName"
                .ValueMember = "ID_Driver"
            End With
        End If

    End Sub

    Public Sub FillDriverAllCombo(ByVal combo As ComboBox)
        Dim drivers = From a In db.tblDrivers
                        Select a
                        Where a.IsActive = True
        Dim emptyTable As tblDriver() = {New tblDriver With {.ID_Driver = 0, .DriverName = "-------"}}
        Dim drivers2 = (From e In emptyTable Select e).Union(drivers).ToList
        With combo
            .DataSource = drivers2
            .DisplayMember = "DriverName"
            .ValueMember = "ID_Driver"
        End With
    End Sub
    Public Sub FillTruckAllCombo(ByVal combo As ComboBox)
        Dim trucks = From a In db.tblTrucks
                     Where a.Type = 1
                        Select a
        Dim emptyTable As tblTruck() = {New tblTruck With {.ID_Truck = 0, .TruckName = "-------"}}
        Dim trucks2 = (From e In emptyTable Select e).Union(trucks).ToList
        With combo
            .DataSource = trucks2
            .DisplayMember = "TruckName"
            .ValueMember = "ID_Truck"
        End With
    End Sub
    Public Sub FillFeeCombo(ByVal combo As ComboBox, ByVal id_feeType As Integer)
        Dim query = From a In db.tblFees
                     Where a.tblFeeType.ID_FeeType = id_feeType
                        Select a.FeeName, a.ID_Fee
        ' Dim emptyTable As tblTruck() = {New tblTruck With {.ID_Truck = 0, .TruckName = "-------"}}
        ' Dim trucks2 = (From e In emptyTable Select e).Union(trucks).ToList
        With combo
            .DataSource = query
            .DisplayMember = "FeeName"
            .ValueMember = "ID_Fee"
        End With
    End Sub
    Public Sub FillSellingFeeCombo(ByVal combo As DataGridViewComboBoxColumn)
        Dim query = From a In db.tblFees
                     Where a.tblFeeType.ID_FeeType = 2
                        Select a.FeeName, a.ID_Fee
        '  
        With combo
            .DataSource = query
            .DisplayMember = "FeeName"
            .ValueMember = "ID_Fee"
        End With
    End Sub
    Public Sub FillAllFeeCombo(ByVal combo As ComboBox, ByVal id_feeType As Integer)
        Dim query = From a In db.tblFees
                     Where a.tblFeeType.ID_FeeType = id_feeType
                        Select a
        'Dim emptyTable As tblFee() = {New tblFee With {.ID_Fee = 0, .FeeName = "------"}}
        'Dim query2 = (From e In emptyTable Select e).Union(query).ToList
        With combo
            .DataSource = query
            .DisplayMember = "FeeName"
            .ValueMember = "ID_Fee"
        End With
    End Sub
    Public Sub FillFeeTree(ByVal tree As DevComponents.AdvTree.AdvTree)
        Dim query = From a In db.tblFees
                        Select a.FeeName, a.ID_Fee
        ' 
        With tree
            .DataSource = query
            .DisplayMembers = "ID_Fee,FeeName"
            .ValueMember = "ID_Fee"
        End With
    End Sub

    Public Sub FillFeeTree(ByVal tree As System.Windows.Forms.TreeView, ByVal idfeetype As Integer)


        ' Add a root node.
        Dim node As TreeNode

        node = tree.Nodes.Add("Check All")
        Dim query = From a In db.tblFees
                    Where a.ID_FeeType = idfeetype And a.IsActive = True
                        Select New With {a.ID_Fee, a.FeeName}
        For Each a In query

            node.Nodes.Add(a.ID_Fee, String.Format(a.FeeName))

        Next


    End Sub
    Public Sub FillFeeTree(ByVal tree As System.Windows.Forms.TreeView, ByVal idfeetype As Integer, ByVal idFile As Integer)


        ' Add a root node.
        Dim node As TreeNode

        node = tree.Nodes.Add("Check All")
        Dim query = From a In db.tblFees
                    Where a.ID_FeeType = idfeetype And a.IsActive = True And Not (From b In db.tblFileFees Where b.ID_File = idFile And b.tblFee.ID_FeeType = idfeetype And (b.ID_AdvanceVoucher.HasValue)
                                                            Select b.ID_Fee).Contains(a.ID_Fee)
                    Order By a.Rank Ascending
                    Select New With {a.ID_Fee, a.FeeName}
        For Each a In query

            node.Nodes.Add(a.ID_Fee, String.Format(a.FeeName))

        Next


    End Sub
    ''' <summary>
    ''' Lấy ra các file chưa được thanh toán để tạm ứng
    ''' </summary>
    ''' <param name="tree"></param>
    ''' <remarks></remarks>
    Public Sub FillFileTree(ByVal tree As System.Windows.Forms.TreeView)

        'File chưa được thanh toán, là những file mà file fee có giá trị id_phiếu chi = null

        'Lấy các file
        'Dim listFile = From c In db.tblFees
        '                 Where c.ID_FeeType = 1 And Not (From b In db.tblFileFees
        '                                                  Where b.tblFile.ID_File = idFile And b.IsBuying = True
        '                                                  Select b.ID_Fee).Contains(c.ID_Fee)
        '                Select c.ID_Fee



        ' Add a root node.
        'Dim node As TreeNode
        'node = tree.Nodes.Add("Export File")

        'Dim query = From c In db.tblFiles
        '                 Where c.IsExport = True And c.FileNo IsNot Nothing And Not (From b In db.tblFileFees
        '                                                  Where b.ID_ReceiptVoucher IsNot Nothing
        '                                                  Select b.ID_File).Contains(c.ID_File)
        '                Order By c.FileNo Descending
        '                Select c.ID_File, c.FileNo
        'For Each a In query

        '    ' Add a node as a child of the previously added node.
        '    node.Nodes.Add(a.ID_File, String.Format(a.FileNo))

        'Next
        'node = tree.Nodes.Add("Import File")

        'query = From c In db.tblFiles
        '                 Where c.IsExport = False And c.FileNo IsNot Nothing And Not (From b In db.tblFileFees
        '                                                  Where b.ID_ReceiptVoucher IsNot Nothing
        '                                                  Select b.ID_File).Contains(c.ID_File)
        '                Order By c.FileNo Descending
        '                Select c.ID_File, c.FileNo
        'For Each a In query

        '    ' Add a node as a child of the previously added node.
        '    node.Nodes.Add(a.ID_File, String.Format(a.FileNo))

        'Next


    End Sub
    Public Sub FillContTree(ByVal tree As System.Windows.Forms.TreeView, ByVal id_file As Integer)

        Dim type = From b In db.tblConts
                   Where b.tblHBL.tblFileMBL.ID_File = id_file
                   Select b.tblContType


        ' Add nodes to treeView1. 
        Dim node As TreeNode
        node = tree.Nodes.Add("Check All")
        'Dim index = 0
        ' For Each b As tblContType In type
        ' Add a root node.

        'node.Nodes.Add(String.Format(b.ContTypeName))

        Dim query = From a In db.tblConts
                Where a.tblHBL.tblFileMBL.ID_File = id_file
                Order By a.ContNo
                    Select New With {a.ID_Cont, a.ContNo}
        For Each a In query

            ' Add a node as a child of the previously added node.
            node.Nodes.Add(a.ID_Cont, String.Format(a.ContNo))

            ' Next
            'index += 1
        Next

    End Sub
    Public Sub FillFeeCombo(ByVal combo As DataGridViewComboBoxColumn, ByVal id_feeType As Integer)
        Dim query = From a In db.tblFees
                     Where a.tblFeeType.ID_FeeType = id_feeType
                        Select a.FeeName, a.ID_Fee
        With combo
            .DataSource = query
            .DisplayMember = "FeeName"
            .ValueMember = "ID_Fee"
        End With
    End Sub

    Sub FillGoodCombo(ByVal combo As ComboBox)
        Dim query As Object


        query = From a In db.tblGoods
                        Select a.ID_Good, a.GoodName
        ' Order By Name Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "GoodName"
                .ValueMember = "ID_Good"
            End With
        End If

    End Sub

    Sub FillUnitCombo(ByVal combo As ComboBox)
        Dim query As Object


        query = From a In db.tblUnits
                        Select a.ID_Unit, a.UnitName
        ' Order By Name Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "UnitName"
                .ValueMember = "ID_Unit"
            End With
        End If

    End Sub
    Function GetNameUnit(ByVal idUnit As Integer) As String
        Dim query As String


        query = (From a In db.tblUnits
                 Where a.ID_Unit = idUnit
                        Select a.UnitName).First()
        ' Order By Name Ascending

        Return query

    End Function
    Function GetCurrencyOfFile(ByVal idFile As Integer) As tblCurrency

        Dim query As Integer
        Try
            query = (From a In db.tblFiles
             Where a.ID_File = idFile
             Select a.ID_Currency).First

            Dim re = (From b In db.tblCurrencies
                     Where b.ID_Currency = query
                     Select b).Single


            Return re
        Catch ex As Exception

        End Try

    End Function

    Function GetCurrencyOfBuying(ByVal idFile As Integer) As IQueryable(Of tblCurrency)

        Dim query
        Try
            query = (From a In db.tblFileFees
             Where a.ID_File = idFile And Not a.tblCurrency.Code.Contains("VND")
             Select a.tblCurrency).Distinct
            Return query
        Catch ex As Exception

        End Try

    End Function
    ''' <summary>
    ''' Trả về True nếu là Phí không tính theo container
    ''' </summary>
    ''' <param name="idFee"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function IsBillFee(ByVal FeeName As String) As Boolean
        'Phí selling
        Dim q As Boolean
        q = (From b In db.tblFees
           Where b.FeeName = FeeName And b.ID_FeeType = 2
           Select b.IsBill).First()
        Return q
    End Function
    Sub FillUnitCombo(ByVal combo As DataGridViewComboBoxColumn)
        Dim query As Object


        query = From a In db.tblUnits
                        Select a.ID_Unit, a.UnitName
        ' Order By Name Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "UnitName"
                .ValueMember = "ID_Unit"
            End With
        End If

    End Sub

    Sub FillFeeTypeCombo(ByVal combo As ComboBox)
        Dim query As Object


        query = From a In db.tblFeeTypes
                Where a.ID_FeeType = 1 Or a.ID_FeeType = 2
                        Select a.ID_FeeType, a.FeeTypeName
        ' Order By Name Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "FeeTypeName"
                .ValueMember = "ID_FeeType"
            End With
        End If

    End Sub

    Sub FillTruckCombo(ByVal combo As ComboBox)
        Dim query As Object

        query = From a In db.tblTrucks
                Where a.Type = 1 And a.IsActive = True
                    Select a.ID_Truck, a.TruckNumber
                    Order By TruckNumber Ascending

        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "TruckNumber"
                .ValueMember = "ID_Truck"
            End With
        End If

    End Sub


    Sub FillTruckCombo(ByVal combo As DataGridViewComboBoxColumn)
        Dim query As Object

        query = From a In db.tblTrucks
                Where a.Type = 1
                    Select a.ID_Truck, a.TruckNumber
                    Order By TruckNumber Ascending


        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "TruckNumber"
                .ValueMember = "ID_Truck"
            End With
        End If
    End Sub

    Sub FillDriverCombo(ByVal combo As DataGridViewComboBoxColumn)
        Dim query As Object

        ' If My.Application.ActiveRoleId = 2 Then
        query = From a In db.tblDrivers
                  Select a.ID_Driver, a.DriverName
                     Order By DriverName Ascending
        'Else

        'query = From a In db.customers
        '            Select a.id, a.name
        '            Order By name Ascending
        'End If


        If Not IsNothing(query) Then
            With combo
                .DataSource = query
                .DisplayMember = "DriverName"
                .ValueMember = "ID_Driver"
            End With
        End If
    End Sub

    Sub FillVoyageCombo(ByVal combo As ComboBox)
        Dim query = From a In db.tblVoyages
                    Select a.ID_Voyage, a.VoyageNo

        With combo
            .DataSource = query
            .DisplayMember = "VoyageNo"
            .ValueMember = "ID_Voyage"
        End With
    End Sub

    Sub FillCurrencyCombo(ByVal combo As ComboBox)
        Dim query = From a In db.tblCurrencies
                       Select a.ID_Currency, a.Code


        With combo
            .DataSource = query
            .DisplayMember = "Code"
            .ValueMember = "ID_Currency"
        End With
    End Sub

    Sub FillCurrencyCombo(ByVal combo As DataGridViewComboBoxColumn)
        Dim query = From a In db.tblCurrencies
                       Select a.ID_Currency, a.Code


        With combo
            .DataSource = query
            .DisplayMember = "Code"
            .ValueMember = "ID_Currency"
        End With
    End Sub

    Sub FillUserCombo(ByVal combo As ComboBox)
        Dim query = From a In db.tblUsers
                       Select a.ID_User, a.FullName


        With combo
            .DataSource = query
            .DisplayMember = "FullName"
            .ValueMember = "ID_User"
        End With
    End Sub
    Sub FillUserCombo(ByVal combo As ComboBox, ByVal role As Integer)
        Dim query = From a In db.tblUsers
                    Where a.ID_Role = role
                       Select a.ID_User, a.FullName


        With combo
            .DataSource = query
            .DisplayMember = "FullName"
            .ValueMember = "ID_User"
        End With
    End Sub



    Sub FillAllDriverCombo(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree)
        Dim query = From a In db.tblDrivers
                       Select a.ID_Driver, a.DriverName, a.PhoneNo

        With combo
            .DataSource = query
            .DisplayMembers = "DriverName,PhoneNo"
            .SelectedDisplayMemeber = "DriverName"
            .ValueMember = "ID_Driver"
        End With
    End Sub

    Sub FillAllUserCombo(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree)
        Dim query = From a In db.tblUsers
                       Select a.ID_User, a.UserName, a.tblRole.RoleName

        With combo
            .DataSource = query
            .DisplayMembers = "UserName,RoleName"
            .SelectedDisplayMemeber = "UserName"
            .ValueMember = "ID_User"
        End With
    End Sub

    Sub FillAdvanceVoucherCombo(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree)

        Dim query = From a In db.tblAdvanceVouchers
                    Order By a.ID_AdvanceVoucher Descending
                       Select a.ID_AdvanceVoucher, a.AdvanceVoucherNo, a.tblUser.UserName

        With combo
            .DataSource = query
            .DisplayMembers = "FullName,AdvanceVoucherNo"
            .SelectedDisplayMemeber = "AdvanceVoucherNo"
            .ValueMember = "ID_AdvanceVoucher"
        End With
    End Sub

    Sub FillCustomerCombo(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree)
        Dim query = From a In db.tblCustomers
                       Select a.ID_Customer, a.CustomerName, a.Tel


        With combo
            .DataSource = query
            .DisplayMembers = "CustomerName,CompanyName,Tel"
            .SelectedDisplayMemeber = "CustomerName"
            .ValueMember = "ID_Customer"
        End With
    End Sub

    Sub FillPhieuTamUngCombo(ByVal combo As DataGridViewComboBoxColumn)
        Dim query1 = From a In db.tblAdvanceVouchers
                       Select a 'a.ID_AdvanceVoucher, a.AdvanceVoucherNo

        Dim emptyTable As tblAdvanceVoucher() = {New tblAdvanceVoucher With {.ID_AdvanceVoucher = 0, .AdvanceVoucherNo = "-------"}}
        Dim query = (From e In emptyTable Select e).Union(query1).ToList
        With combo
            .DataSource = query
            .DisplayMember = "AdvanceVoucherNo"
            .ValueMember = "ID_AdvanceVoucher"
        End With
    End Sub


    Sub FillPhieuChiCombo(ByVal combo As DataGridViewComboBoxColumn)
        Dim query1 = From a In db.tblPaymentVouchers
                       Select a 'a.ID_PaymentVoucher, a.PaymentVoucherNo

        Dim emptyTable As tblPaymentVoucher() = {New tblPaymentVoucher With {.ID_PaymentVoucher = 0, .PaymentVoucherNo = "-------"}}
        Dim query = (From e In emptyTable Select e).Union(query1).ToList
        With combo
            .DataSource = query
            .DisplayMember = "PaymentVoucherNo"
            .ValueMember = "ID_PaymentVoucher"
        End With
    End Sub

    Sub FillPhieuThuCombo(ByVal combo As DataGridViewComboBoxColumn)
        Dim query1 = From a In db.tblReceiptVouchers
                       Select a 'a.ID_ReceiptVoucher, a.ReceiptVoucherNo

        Dim emptyTable As tblReceiptVoucher() = {New tblReceiptVoucher With {.ID_ReceiptVoucher = 0, .ReceiptVoucherNo = "-------"}}
        Dim query = (From e In emptyTable Select e).Union(query1).ToList
        With combo
            .DataSource = query
            .DisplayMember = "ReceiptVoucherNo"
            .ValueMember = "ID_ReceiptVoucher"
        End With
    End Sub

    Sub FillPhieuTamUngCombo(ByVal combo As ComboBox)
        Dim query1 = From a In db.tblAdvanceVouchers
                       Select a 'a.ID_AdvanceVoucher, a.AdvanceVoucherNo

        Dim emptyTable As tblAdvanceVoucher() = {New tblAdvanceVoucher With {.ID_AdvanceVoucher = 0, .AdvanceVoucherNo = "-------"}}
        Dim query = (From e In emptyTable Select e).Union(query1).ToList
        With combo
            .DataSource = query
            .DisplayMember = "AdvanceVoucherNo"
            .ValueMember = "ID_AdvanceVoucher"
        End With
    End Sub

    Sub FillPhieuChiCombo(ByVal combo As ComboBox)
        Dim query1 = From a In db.tblPaymentVouchers
                       Select a 'a.ID_PaymentVoucher, a.PaymentVoucherNo

        Dim emptyTable As tblPaymentVoucher() = {New tblPaymentVoucher With {.ID_PaymentVoucher = 0, .PaymentVoucherNo = "-------"}}
        Dim query = (From e In emptyTable Select e).Union(query1).ToList
        With combo
            .DataSource = query
            .DisplayMember = "PaymentVoucherNo"
            .ValueMember = "ID_PaymentVoucher"
        End With
    End Sub

    Sub FillPhieuThuCombo(ByVal combo As ComboBox)
        Dim query1 = From a In db.tblReceiptVouchers
                       Select a 'a.ID_ReceiptVoucher, a.ReceiptVoucherNo

        Dim emptyTable As tblReceiptVoucher() = {New tblReceiptVoucher With {.ID_ReceiptVoucher = 0, .ReceiptVoucherNo = "-------"}}
        Dim query = (From e In emptyTable Select e).Union(query1).ToList
        With combo
            .DataSource = query
            .DisplayMember = "ReceiptVoucherNo"
            .ValueMember = "ID_ReceiptVoucher"
        End With
    End Sub



    Public Sub FillVoyageList(ByVal combo As DevComponents.DotNetBar.Controls.ComboTree, ByVal idPOL As Integer, ByVal idPOD As Integer, ByVal ETD As DateTime, ByVal ETA As DateTime)
        Dim query
        Try

            query = From b In db.tblVoyages
                   Where b.ID_POD = idPOD And b.ID_POL = idPOL And b.ETA = ETA And b.ETD = ETD
                   Select b.ID_Voyage, b.VoyageNo, b.tblVessel.VesselName
            With combo
                .DataSource = query
                .DisplayMembers = "VoyageNo,VesselName"
                .SelectedDisplayMemeber = "VoyageNo"
                .ValueMember = "ID_Voyage"
            End With
        Catch ex As Exception

        End Try
    End Sub
    Public Function FormatWithoutSymbol(ByVal s As Double) As String
        'Establishes a formatter'
        Dim nfi As NumberFormatInfo = CultureInfo.CurrentCulture.NumberFormat
        'Clones the instance (required)'
        nfi = DirectCast(nfi.Clone(), NumberFormatInfo)

        'Sets the currency symbol to nothing'
        nfi.CurrencySymbol = ""

        'Formats the string'
        Return String.Format(nfi, "{0:N0}", s)
    End Function
    Public Function FormatMoney(ByVal money As Object) As String
        'Dim str As String = money.ToString()
        'Dim pattern As String = "(?<a>\d*)(?<b>\d{3})*"
        'Dim m As Match = Regex.Match(Str, pattern, RegexOptions.RightToLeft)
        'Dim sb As StringBuilder = New StringBuilder

        'For Each i As Capture In m.Groups("b").Captures
        '    sb.Insert(0, "," + i.Value)
        'Next

        'sb.Insert(0, m.Groups("a").Value)
        'Return sb.ToString().Trim(",")
        Return FormatWithoutSymbol(money)
        'Return money.Format("{0:0,0}")
    End Function
    Public Sub FormatCurr(ByVal txt As TextBox)
        Try
            Dim str As String = txt.Text
            Dim start As Integer = txt.Text.Length - txt.SelectionStart
            str = str.Replace(",", "")
            txt.Text = FormatMoney(str)
            txt.SelectionStart = -start + txt.Text.Length
        Catch ex As Exception

        End Try

    End Sub

    Public Function GetTime_HangTau() As Integer
        Dim num As Integer = (From a In db.tblConfigs
                              Where a.ConfigName = "Time_HangTau"
                             Select a.Value).First()
        Return num
    End Function
    Public Function GetTime_DuoiCang() As Integer
        Dim num As Integer = (From a In db.tblConfigs
                              Where a.ConfigName = "Time_DuoiCang"
                             Select a.Value).First()
        Return num
    End Function
    Public Function GetTime_KhachHang() As Integer
        Dim num As Integer = (From a In db.tblConfigs
                              Where a.ConfigName = "Time_KhachHang"
                             Select a.Value).First()
        Return num
    End Function
End Class

